/**
 * Copyright (C) 2017-2018 pinganfang, Inc. All Rights Reserved.
 */
package com.pingan.haofang.searchcloud.api.jdbc;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import com.pingan.haofang.searchcloud.api.IndexRowData;

/**
 * @author huhailiang937
 * @date 2017年5月5日下午4:53:48
 * @since 1.0.0
 */
public class ResultSetTransfer {

    public static List<IndexRowData> decode(ResultSet resultSet) throws SQLException {
        List<IndexRowData> indexRowDatas = new LinkedList<IndexRowData>();
        // 获得结果集结构信息,元数据
        ResultSetMetaData md = resultSet.getMetaData();
        // 获得列数
        int columnCount = md.getColumnCount();
        while (resultSet.next()) {
            IndexRowData rowData = new IndexRowData();
            for (int i = 1; i <= columnCount; i++) {
                rowData.put(md.getColumnLabel(i), resultSet.getObject(i));
            }
            indexRowDatas.add(rowData);
        }
        return indexRowDatas;
    }
    
    /**
     * 
     * @param resultSet
     * @param indexName
     * @return
     * @throws SQLException
     *
     */
    public static List<IndexRowData> decode(ResultSet resultSet,String indexName) throws SQLException {
        List<IndexRowData> indexRowDatas = new LinkedList<IndexRowData>();
        // 获得结果集结构信息,元数据
        ResultSetMetaData md = resultSet.getMetaData();
        // 获得列数
        int columnCount = md.getColumnCount();
        
        // TODO 根据indexName 获取主键
        
        while (resultSet.next()) {
            IndexRowData rowData = new IndexRowData();
            for (int i = 1; i <= columnCount; i++) {
                rowData.put(md.getColumnName(i), resultSet.getObject(i));
            }
            indexRowDatas.add(rowData);
        }
        return indexRowDatas;
    }
}
